<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: AddPortlet.php 396 2010-12-25 13:25:12Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalPortletRecord');

class AddPortlet extends PortalSystemPage
{

	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,"You need to be logged in to use this page");
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,"You need to be logged in to use this page");
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->Title = $this->Application->Parameters['SiteTitle'] . " - ". Prado::localize("Add element");

		if(!$this->IsPostBack)
		{
			$pid	= $this->Request['pid'];

			if($pid !== null)
			{
				$this->pid->Value = $pid;
			}
			
			$cid	= $this->Request['cid'];
			
			if($cid !== null)
			{
				$this->cid->Value = $cid;
			}

			$portlets = PortalPortletRecord::finder()->findAllByactive(1);
			if($portlets)
			{
				$p = array();

				foreach($portlets as $portlet)
				{
					if(!class_exists($portlet->name,false))
					{
						$class_filename	= Prado::getPathOfNamespace('Application.Portlets.' . $portlet->name . '.Common.' . $portlet->name . 'Common') . '.php';
						if(file_exists($class_filename))
						{
							$incFile = 'Application.Portlets.' . $portlet->name . '.Common.' . $portlet->name . 'Common';
							Prado::using($incFile);
							$pCommonClassName	= $portlet->name . 'Common';

							if(method_exists($pCommonClassName,'getPortletName'))
							{
								$pDisplayName = call_user_func($pCommonClassName . '::getPortletName');
								$p[] = array("name" => $portlet->name, "displayname" => $pDisplayName);
							}
							else
							{
								$p[] = array("name" => $portlet->name, "displayname" => $portlet->displayname);
							}
						}
					}
				}

				$this->PortletName->DataSource	= $p;
				$this->PortletName->dataBind();
			}
			$this->CancelLink->NavigateUrl		= $this->Service->constructUrl($this->pid->Value,array("PageMode" => $this->Page->EditMode));
		}
	}

	public function saveButtonClicked($sender,$param)
	{
		$pid		= $this->pid->Value;
		$cid		= $this->cid->Value;
		$portlet	= $this->PortletName->getSelectedValue();
		$culture	= $this->Application->Culture;
		$nextorder	= $this->DataAccess->createCommand("select count(*) from tblpagecontent where pagename='$pid' && controlname='$cid'")->queryScalar();
		$nextorder++;
		$this->DataAccess->createCommand("insert into tblpagecontent(pagename,controlname,portlet,displayorder,culture) values('$pid','$cid','$portlet',$nextorder,'$culture')")->execute();
		$this->gotoPage($pid,array("PageMode" => $this->EditMode));
	}
}
?>